package com.sofwin.controller;

import com.github.pagehelper.PageInfo;
import com.sofwin.base.ResponseEntity;
import com.sofwin.pojo.SysDept;
import com.sofwin.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @packageName: com.sofwin.controller
 * @author: andyliu
 * @date: 2022/8/10 14:37
 * @email 115176513@qq.com
 * @description: TODO
 */
@Controller
@RequestMapping("/dept")
public class DeptController {
    @Autowired
    private DeptService deptService;

    @GetMapping("/toList")
    public String toList(){
        return "dept/list";
    }

    @GetMapping("/toEdit")
    public String toEidt(Integer id, ModelMap map){
        if(id!=null){
            map.put("dept",deptService.queryById(id));
        }
        return "dept/edit";
    }

    @GetMapping()
    @ResponseBody
    public ResponseEntity data(SysDept dept){
        List<SysDept> depts = deptService.queryDepts(dept);
        return ResponseEntity.success(depts);
    }
    @PostMapping("/save")
    @ResponseBody
    public ResponseEntity save(SysDept dept){
        boolean status = deptService.saveDept(dept);
        if(status) {return ResponseEntity.success();}
        return ResponseEntity.error();
    }
    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public ResponseEntity delete(@PathVariable Integer id){
        boolean status = deptService.removeById(id);
        if(status) {return ResponseEntity.success();}
        return ResponseEntity.error();
    }

}
